package com.zzpct.wh.work.example;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.solver.widgets.analyzer.VerticalWidgetRun;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzpct.wh.work.example.entity.RecipeMode1;
import com.zzpct.wh.work.example.entity.ResultsBean;
import com.zzpct.wh.work.example.service.MyAppservice;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    List<ResultsBean> allResultsBeanlist;
    private static final String TAG = "MainActivity";
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://liyuankun.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ListView listView;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv);
        getRecipeInfo();
        getREcipeInfoFromDB();
        MyAdapter myAdapter =new MyAdapter();
        listView.setAdapter(myAdapter);


    }

    private void getREcipeInfoFromDB() {
        allResultsBeanlist = LitePal.findAll(ResultsBean.class);
    }

    private void getRecipeInfo() {
        //构造接口对象
       MyAppservice myAppservice=retrofit.create(MyAppservice.class);
       //调用接口中的方法 返回Call类型
        Call<RecipeMode1>allRecipe=myAppservice.getAllRecipe(5);
        allRecipe.enqueue(new Callback<RecipeMode1>() {
            @Override
            public void onResponse(Call<RecipeMode1> call, Response<RecipeMode1> response) {
                RecipeMode1 recipeMode1=response.body();
                Log.d(TAG, "onResponse: "+recipeMode1.getResults().size());
                //将数据放入sqlite数据库中    使用litepal数据库框架
                List<ResultsBean> results = recipeMode1.getResults();
                for (ResultsBean r:results){
                    r.save();
                    Log.d(TAG, "onResponse: "+r.save());
                }
            }

            @Override
            public void onFailure(Call<RecipeMode1> call, Throwable t) {

            }
        });
    }
    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return allResultsBeanlist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v=View.inflate(MainActivity.this,R.layout.recipemodel_item,null);
            ImageView iv_pic=v.findViewById(R.id.iv_pic);
            TextView tv_titel=v.findViewById(R.id.tv_title);
            ResultsBean resultsBean=allResultsBeanlist.get(position);
            String cover =resultsBean.getCover();
            Glide.with(MainActivity.this)
                    .load(cover)
                    .into(iv_pic);
            tv_titel.setText(resultsBean.getTitle());

            return v;
        }
    }
}
